package com.dhcy.mqtt.service.impl;


import com.dhcy.mqtt.par.ht.*;
import com.dhcy.mqtt.result.ht.TableResultData;
import com.dhcy.mqtt.util.AppResultData;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcy.mqtt.dao.OperationLogMapper;
import com.dhcy.mqtt.model.OperationLog;
import com.dhcy.mqtt.service.OperationLogService;
import java.util.ArrayList;
import com.dhcy.mqtt.util.IsNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.dhcy.mqtt.result.ht.OperationLogResult;

@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService{
@Autowired
    OperationLogMapper operationLogMapper;

    @Override
    public TableResultData operationLogList(OperationLogListPar operationLogListPar) {
        IPage<OperationLog> page = new Page<>(operationLogListPar.getPage(), operationLogListPar.getPageSize());
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        if(!IsNull.isNullOrEmpty(operationLogListPar.getMessage())){
            queryWrapper.like("message",operationLogListPar.getMessage());
        }
        if(!IsNull.isNullOrEmpty(operationLogListPar.getCreateUser())){
            queryWrapper.like("createUser",operationLogListPar.getCreateUser());
        }
        page = operationLogMapper.selectPage(page, queryWrapper);
        List<OperationLogResult> resultList = new ArrayList<>();
        for (OperationLog operationLog : page.getRecords()) {
            OperationLogResult operationLogResult = new OperationLogResult();
            BeanUtils.copyProperties(operationLog, operationLogResult);
            resultList.add(operationLogResult);
        }
        return TableResultData.success(page.getTotal(), resultList);
    }

    @Override
    public AppResultData operationLogAdd(OperationLogAddPar operationLogaddPar) {
        OperationLog operationLog = new OperationLog();
        BeanUtils.copyProperties(operationLogaddPar, operationLog);
        if (IsNull.isNullOrEmpty(operationLog.getId())) {
            operationLog.setCreateTime(new Date());
            operationLogMapper.insert(operationLog);
        } else {
            operationLogMapper.updateById(operationLog);
        }
        return AppResultData.successMessage("成功");
    }

    @Override
    public AppResultData operationLogDel(Integer id) {
        int count = operationLogMapper.deleteById(id);
        if (count > 0)
            return AppResultData.successMessage("成功");
        else {
            return AppResultData.errorMessage("当前记录不存在");
        }
    }}
